<?php

require_once 'gestorconfiguracion.php';

function openConnection() {
    $db = new mysqli('localhost', 'root', '', 'co01-coches');
    if ($db->connect_errno > 0) {
        die('Unable to connect to database [' . $db->connect_error . ']');
    }
    return $db;
}

function closeConnection($db) {
    $db->close();
    return;
}

function getListadoMarcas() {
    $db = openConnection();
    //Conexion con la BBDD sin utilizar funciones componentes externas
    //$db = new mysqli('localhost', 'root', '', 'co01-coches');
    //if ($db->connect_errno > 0) {
    //die('Unable to connect to database [' . $db->connect_error . ']');
    //    }
    //Sentencia Sq
    $sql = "SELECT ID,MARCA from co01_marcas";
    $result = $db->query($sql);

    //Desconexion con la BBDD sin utilizar funciones componentes externas   
    //$db->close();
    closeConnection($db);
    return $result;
}

function getListaClaseEnergetica() {
    $db = openConnection();
//Sentencia Sq
    $sql = "SELECT DISTINCT C_ENERGETICA from co01_modelos ORDER BY C_ENERGETICA ASC";
    $result=$db->query($sql);
    closeConnection($db);
    return $result;
}

function deleteModelo($id_modelo) {
 
    $sql_borrado = "DELETE FROM CO01_MODELOS WHERE ID=" . $id_modelo;
    $db = openConnection();
    $db->query($sql_borrado);
    $num_filas = $db->affected_rows;
    closeConnection($db);
    return $num_filas;
}

function  modificarModelo($idmodelo,$modelo,$marca){
    $sql_modificado = "UPDATE CO01_MODELOS SET MODELO=\"" . $modelo ."\" AND SET MARCA=\"" . $marca . "\" WHERE ID=" . $idmodelo;
    
    $db = openConnection();
    $db->query($sql_modificado);
    $num_filas = $db->affected_rows;
    error_log("\n\n" . $num_filas . "****" . $sql_modificado,3,"C:\wamp\www\COCHESBBDD\mis_logs.txt");
    
    closeConnection($db);
    return $num_filas;
   
}
function getModelos($marca, $consumo, $emisiones, $pagina, $campo_orden, $tipo_orden) {
    global $num_regs;
    $startrow = $pagina * getNumRegistroPorPagina();
    $db = openConnection();


    if ($marca == 0) {

        $sql = "SELECT A.MARCA AS ID,B.MARCA, MODELO, CONSUMO, EMISIONES, C_ENERGETICA, A.ID AS ID_MODELO FROM" .
                " co01_modelos AS A INNER JOIN co01_marcas AS B ON A.MARCA = B.ID" .
                " WHERE CONSUMO<=" . $consumo . " AND EMISIONES<=" . $emisiones .
                " ORDER BY " . $campo_orden . " " . $tipo_orden .
                " LIMIT " . $startrow . "," . getNumRegistroPorPagina();

        error_log("SELECT COMPLEJA:" . $sql);

        //$sql = "SELECT * FROM CO01_MODELOS WHERE CONSUMO<=" . $consumo . " AND EMISIONES<=" . $emisiones. " LIMIT " . $startrow . "," . getNumRegistroPorPagina();
        $sql_num_filas = "SELECT  COUNT(*) FROM CO01_MODELOS WHERE CONSUMO<=" .
                $consumo . " AND EMISIONES<=" . $emisiones;
    } else {

        $sql = "SELECT A.MARCA AS ID,B.MARCA, MODELO, CONSUMO, EMISIONES, C_ENERGETICA,A.ID AS ID_MODELO FROM" . 
                " co01_modelos AS A INNER JOIN co01_marcas AS B ON A.MARCA = B.ID" . 
                " WHERE A.MARCA=" . $marca . " AND CONSUMO<=" . $consumo . " AND EMISIONES<=" . 
                $emisiones . " LIMIT " . $startrow . "," . getNumRegistroPorPagina();

        //$sql = "SELECT * FROM CO01_MODELOS WHERE MARCA=" . $marca . " AND CONSUMO<=" . $consumo . " AND EMISIONES<=" . $emisiones. " LIMIT " . $startrow . "," . getNumRegistroPorPagina();
        $sql_num_filas = "SELECT COUNT(*) FROM CO01_MODELOS WHERE MARCA=" . $marca . " AND CONSUMO<=" . $consumo . " AND EMISIONES<=" . $emisiones;
    }
    $rows_num_filas = $db->query($sql_num_filas);
    //  error_log("\n\n" .$sql_num_filas,3,"C:\wamp\www\COCHESBBDD\mis_logs.txt");
    $row_num_filas = mysqli_fetch_array($rows_num_filas);
    $num_regs = $row_num_filas[0];
    //   error_log($num_regs ,3,   "C:\wamp\www\COCHESBBDD\mis_logs.txt");
    $result = $db->query($sql);
    //   error_log("\n\n" .$sql,3,"C:\wamp\www\COCHESBBDD\mis_logs.txt");
    closeConnection($db);
    return $result;
}

?>